package com.huaguoguo.cyg.converter;

import cn.hutool.core.convert.Converter;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.huaguoguo.cyg.entity.weibo.to.WBDetail;
import com.huaguoguo.cyg.entity.weibo.to.WBMediaWrap;
import com.huaguoguo.cyg.entity.weibo.to.WBPic;
import lombok.SneakyThrows;

import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * action-data属性的数据转换器
 * @author Administrator
 */
public class StringActionDataConverter implements Converter<WBMediaWrap> {


    /**
     * 转换为图片url<br>
     * 如果类型无法确定，将读取默认值的类型做为目标类型
     *
     * @param value        原始值
     * @param defaultValue 默认值
     * @return 转换后的值
     * @throws IllegalArgumentException 无法确定目标类型，且默认值为{@code null}，无法确定类型
     */
    @SneakyThrows
    @Override
    public WBMediaWrap convert(Object value, WBMediaWrap defaultValue) throws IllegalArgumentException {
        // \"over9pic=1&isPrivate=0&relation=0&clear_picSrc=%2F%2Fwx1.sinaimg.cn%2Fmw690%2F007n5mD4ly1gah4ndpoz9j31401hctsx.jpg,%2F%2Fwx4.sinaimg.cn%2Fmw690%2F007n5mD4ly1gah4ne1zbnj31401hcasu.jpg,%2F%2Fwx2.sinaimg.cn%2Fmw690%2F007n5mD4ly1gah4nenujyj31401hc7nb.jpg,%2F%2Fwx4.sinaimg.cn%2Fmw690%2F007n5mD4ly1gah4nfltj3j31401hcnbd.jpg,%2F%2Fwx4.sinaimg.cn%2Fmw690%2F007n5mD4ly1gahf8cv1c9j31401hckad.jpg,%2F%2Fwx2.sinaimg.cn%2Fmw690%2F007n5mD4ly1gahakr2rzuj31401hcwzi.jpg,%2F%2Fwx4.sinaimg.cn%2Fmw690%2F007n5mD4ly1gah4nhc3shj31401hc1do.jpg,%2F%2Fwx4.sinaimg.cn%2Fmw690%2F007n5mD4ly1gah4nkdvvwj31401hcngo.jpg,%2F%2Fwx1.sinaimg.cn%2Fmw690%2F007n5mD4ly1gah4ngtd7kj32802yox6q.jpg&thumb_picSrc=%2F%2Fwx1.sinaimg.cn%2Forj360%2F007n5mD4ly1gah4ndpoz9j31401hctsx.jpg,%2F%2Fwx4.sinaimg.cn%2Forj360%2F007n5mD4ly1gah4ne1zbnj31401hcasu.jpg,%2F%2Fwx2.sinaimg.cn%2Forj360%2F007n5mD4ly1gah4nenujyj31401hc7nb.jpg,%2F%2Fwx4.sinaimg.cn%2Forj360%2F007n5mD4ly1gah4nfltj3j31401hcnbd.jpg,%2F%2Fwx4.sinaimg.cn%2Forj360%2F007n5mD4ly1gahf8cv1c9j31401hckad.jpg,%2F%2Fwx2.sinaimg.cn%2Forj360%2F007n5mD4ly1gahakr2rzuj31401hcwzi.jpg,%2F%2Fwx4.sinaimg.cn%2Forj360%2F007n5mD4ly1gah4nhc3shj31401hc1do.jpg,%2F%2Fwx4.sinaimg.cn%2Forj360%2F007n5mD4ly1gah4nkdvvwj31401hcngo.jpg,%2F%2Fwx1.sinaimg.cn%2Fthumb150%2F007n5mD4ly1gah4ngtd7kj32802yox6q.jpg&uid=6754064182&pic_ids=007n5mD4ly1gah4ndpoz9j31401hctsx,007n5mD4ly1gah4ne1zbnj31401hcasu,007n5mD4ly1gah4nenujyj31401hc7nb,007n5mD4ly1gah4nfltj3j31401hcnbd,007n5mD4ly1gahf8cv1c9j31401hckad,007n5mD4ly1gahakr2rzuj31401hcwzi,007n5mD4ly1gah4nhc3shj31401hc1do,007n5mD4ly1gah4nkdvvwj31401hcngo,007n5mD4ly1gah4ngtd7kj32802yox6q&mid=4455959206056346&pic_objects=&object_ids=1042018%3A89a06b23a30a883c92af726ecc0582b1%2C1042018%3Aac0161b4ca00da79055e7da74976d3c1%2C1042018%3A8055bb0c35d0163ce74925fdead8f66c%2C1042018%3A38d67ea54d76f827979645596a0daae0%2C1042018%3A462f0bd5d46b3a27d971574f922d9756%2C1042018%3A3ccbbcf8b9bb9bfbe73abd45276b49c6%2C1042018%3Ae47e431a477e75693a488a9d877df1cd%2C1042018%3A6431785977232850a7876787760a76d0%2C1042018%3A24e126a7af8a373f9808df0a177e4e20&photo_tag_pids=\"
        Map<String, Object> paramMap = new UrlParamConverter().convert(value, null);
        // 解析不到mid就取默认值
        String mid = MapUtil.getStr(paramMap, "mid");
        // 大图的 key -> clear_picSrc
        String clearPicSrcs = MapUtil.getStr(paramMap, "clear_picSrc");
        List<WBPic> clearPicSrcList = null;
        if (StrUtil.isNotEmpty(clearPicSrcs)) {
            clearPicSrcList = Arrays.stream(clearPicSrcs.split(","))
                    .map(s -> "http:" + s)
                    .map(URLDecoder::decode)
                    .map(s -> new WBPic(s))
                    .collect(Collectors.toList());
        }

        // 缩略图的 key -> clear_picSrc
        String thumbPicSrcs = MapUtil.getStr(paramMap, "thumb_picSrc");
        List<WBPic> thumbPicSrcList = null;
        if (StrUtil.isNotEmpty(thumbPicSrcs)) {
            thumbPicSrcList = Arrays.stream(thumbPicSrcs.split(","))
                    .map(s -> "http:" + s)
                    .map(URLDecoder::decode)
                    .map(s -> new WBPic(s))
                    .collect(Collectors.toList());
        }
        return WBMediaWrap.builder().mid(mid).clearPicSrcList(clearPicSrcList).thumbPicSrcList(thumbPicSrcList).build();
    }
}
